package org.edu.modules.common.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.edu.modules.common.entity.VeBaseSpecialty;
import org.edu.modules.common.mapper.VeBaseSpecialtyMapper;
import org.edu.modules.common.service.IVeBaseSpecialtyService;
import org.edu.modules.common.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther 李少君
 * @Date 2021-12-13 16:27
 */
@Service
public class VeBaseSpecialtyServiceImpl extends ServiceImpl<VeBaseSpecialtyMapper, VeBaseSpecialty> implements IVeBaseSpecialtyService {

    @Autowired
    private VeBaseSpecialtyMapper veBaseSpecialtyMapper;

    @Override
    public List<VeBaseSpecialty> specialtyListByFalid(Integer id) {
        List<VeBaseSpecialty> lists = this.veBaseSpecialtyMapper.querySpecialtyListByFalid(id);
        if (lists.size() > 0)
            for (VeBaseSpecialty veBaseSpecialty : lists) {
                if (veBaseSpecialty.getJlny() != null) {
                    veBaseSpecialty.setJlnyName(DateTimeUtil.timestampToDate(veBaseSpecialty.getJlny().intValue()));
                    continue;
                }
                veBaseSpecialty.setJlnyName("");
            }
        return lists;

    }

    @Override
    public List<Map<String, Object>> getSpecialtyPageList(VeBaseSpecialty veBaseSpecialty) {
        List<Map<String, Object>> list = this.veBaseSpecialtyMapper.getSpecialtyPageList(veBaseSpecialty);
        list = setJlnyName(list);
        return list;
    }

    @Override
    public VeBaseSpecialty getSpecialtyByName(Integer id, String name) {
        VeBaseSpecialty veBaseSpecialty = this.veBaseSpecialtyMapper.getSpecialtyByName(id, name);
        if (veBaseSpecialty != null)
            if (veBaseSpecialty.getJlny() != null) {
                veBaseSpecialty.setJlnyName(DateTimeUtil.timestampToDate(veBaseSpecialty.getJlny().intValue()));
            } else {
                veBaseSpecialty.setJlnyName("");
            }
        return veBaseSpecialty;
    }

    @Override
    public VeBaseSpecialty getSpecialtyByBH(Integer id, String bh) {
        VeBaseSpecialty veBaseSpecialty = this.veBaseSpecialtyMapper.getSpecialtyByBH(id, bh);
        if (veBaseSpecialty != null)
            if (veBaseSpecialty.getJlny() != null) {
                veBaseSpecialty.setJlnyName(DateTimeUtil.timestampToDate(veBaseSpecialty.getJlny().intValue()));
            } else {
                veBaseSpecialty.setJlnyName("");
            }
        return veBaseSpecialty;
    }

    @Override
    public List<Map<String, Object>> getSpecialtyStudentStatistics() {
        List<Map<String, Object>> list = this.veBaseSpecialtyMapper.getSpecialtyStudentStatistics();
        List<Map<String, Object>> arrayList = new ArrayList<>();
        for (Map<String, Object> map : list) {
            List<Map<String, Object>> detailList = this.veBaseSpecialtyMapper.getSpecialtyListByPid((Long) map.get("id"));
            if (detailList.size() < 1)
                arrayList.add(map);
        }
        arrayList = setJlnyName(arrayList);
        return arrayList;
    }

    @Override
    public List<Map<String, Object>> getSpecialtyTreeList() {
        List<Map<String, Object>> rootList = this.veBaseSpecialtyMapper.getSpecialtyList(Integer.valueOf(0), null);
        List<Map<String, Object>> bodyList = this.veBaseSpecialtyMapper.getSpecialtyList(null, null);
        if (bodyList != null && !bodyList.isEmpty()) {
            rootList = setJlnyName(rootList);
            Map<Integer, Integer> mapList = new HashMap<>();
            rootList.forEach(spec -> getChild(spec, mapList));
            return rootList;
        }
        return null;
    }

    @Override
    public List<Map<String, Object>> getSpecialtyByZYMC(String zymc) {
        List<Map<String, Object>> list = this.veBaseSpecialtyMapper.getSpecialtyByZYMC(zymc);
        list = setJlnyName(list);
        return list;
    }

    @Override
    public int stopSpecialtyBatch(String[] ids) {
        Map<Integer, Integer> mapList = new HashMap<>();
        List<String> idList = new ArrayList<>();
        for (String id : ids) {
            idList.add(id);
            getSpecialtyListByPid(id, mapList, idList);
        }
        return this.veBaseSpecialtyMapper.stopSpecialtyBatch(idList);
    }

    @Override
    public int deleteSpecialtyBatch(String[] ids) {
        List<String> list = new ArrayList<>();
        for (String s : ids) {
            list.add(s);
        }
        return veBaseSpecialtyMapper.deleteSpecialtyBatch(list);
    }

    @Override
    public int deleteSpecialtyByFalId(String[] ids) {
        return veBaseSpecialtyMapper.deleteSpecialtyByFalId(ids);
    }

    @Override
    public int stopSpecialtyByFalId(String[] falIds) {
        return veBaseSpecialtyMapper.stopSpecialtyByFalId(falIds);
    }

    @Override
    public int updateSpecialtyFalIdById(Integer ids, Integer falId) {
        List<Integer> list = new ArrayList<>();
        list.add(ids);
        return veBaseSpecialtyMapper.updateSpecialtyFalIdById(list, falId);
    }

    @Override
    public List<Map<String, Object>> getGraduatePeopleCount(String specName) {
        return veBaseSpecialtyMapper.getGraduatePeopleCount(specName);
    }

    @Override
    public void importExcel(List<Map<String, Object>> mapList) {
        // 创建 ObjectMapper 实例
        ObjectMapper objectMapper = new ObjectMapper();

        for (Map map: mapList) {
            // 将 Map 转换为 JwKecheng 对象
            VeBaseSpecialty veBaseSpecialty = objectMapper.convertValue(map, VeBaseSpecialty.class);
            this.save(veBaseSpecialty);

        }
    }

    public void getChild(Map<String, Object> map, Map<Integer, Integer> mapList) {
        List<Map<String, Object>> bodyList = this.veBaseSpecialtyMapper.getSpecialtyList(null, null);
        bodyList = setJlnyName(bodyList);
        List<Map<String, Object>> childList = new ArrayList<>();
        bodyList.stream().filter(c -> !mapList.containsKey(c.get("id"))).filter(c -> (Integer.parseInt(c.get("pid").toString()) == Integer.parseInt(map.get("id").toString())))
                .forEach(c -> {
                    mapList.put(Integer.valueOf(Integer.parseInt(c.get("id").toString())), Integer.valueOf(Integer.parseInt(c.get("pid").toString())));
                    getChild(c, mapList);
                    childList.add(c);
                });
        map.put("childList", childList);
    }

    private List<Map<String, Object>> setJlnyName(List<Map<String, Object>> list) {
        if (list.size() > 0)
            for (Map<String, Object> map : list) {
                if (!"".equals(map.get("jlny")) && map.get("jlny") != null) {
                    Long times = Long.valueOf(Long.parseLong(map.get("jlny").toString()));
                    map.put("jlnyName", DateTimeUtil.timestampToDate(times.longValue()));
                    continue;
                }
                map.put("jlnyName", "");
            }
        return list;
    }

    public void getSpecialtyListByPid(String id, Map<Integer, Integer> mapList, List<String> idList) {
        List<Map<String, Object>> bodyList = this.veBaseSpecialtyMapper.getSpecialtyList(null, null);
        bodyList.stream().filter(c -> !mapList.containsKey(c.get("id"))).filter(c -> (Integer.parseInt(c.get("pid").toString()) == Integer.parseInt(id)))
                .forEach(c -> {
                    mapList.put(Integer.valueOf(Integer.parseInt(c.get("id").toString())), Integer.valueOf(Integer.parseInt(c.get("pid").toString())));
                    getSpecialtyListByPid(c.get("id").toString(), mapList, idList);
                    idList.add(c.get("id").toString());
                });
    }
}
